package com.lushixiang.database;

import com.lushixiang.mybatis.entity.user.UserEntity;
import com.lushixiang.mybatis.maps.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
@Slf4j
public class UserDao {
    @Autowired
    UserMapper userMapper;

    // 查询所有用户信息
    public List<UserEntity> selectAllUser() {
        try {
            return userMapper.selectUser();
        }catch (Exception e){
            log.info("查询所有用户发生异常{}",e.toString());
        }
        return null;
    }

    // 按邮件地址查询是否存在
    public UserEntity selectUserByEmailAddr(String emailAddr) {
        try{
            return userMapper.selectUserByEmailAddr(emailAddr);
        }catch (Exception e){
            log.info("查询邮件地址{}，发生异常：{}",emailAddr,e.toString());
        }
        return null;
    }

    // 用户注册
    public boolean inserToUser(UserEntity userEntity) {
        try{
            userMapper.insert(userEntity);
            log.info("{}插入用户表成功",userEntity);
            return true;
        }catch (Exception e){
            log.error("用户：{},插入用户表失败 {} ",userEntity,e.toString());
            return false;
        }
    }

    // 更新用户状态
    public Boolean updateUserStatusByEmailAddr(String emailAddr) {
        try{
            return  userMapper.updateUserStatusByEmailAddr(emailAddr);
        }catch (Exception e){
            log.info("更新邮箱地址{}失败，{}",emailAddr,e.toString());
        }
        return false;
    }

    // 更新用户密码
    public boolean updateUserPasswordByEmailAddr(String emailAddr, String password) {
        try{
            return userMapper.updateUserPasswordByEmailAddr(emailAddr,password);
        }catch (Exception e){
            log.info("根据用户邮箱更新密码字段失败");
            return false;
        }
    }

    // 依据emailAddr选择性传入的值更新
    public boolean updateUserSelectiveByEmailAddr(UserEntity userEntity){
        try{
            log.info("选择性更新用户表:{}",userEntity);
            return userMapper.updateUserSelectiveByEmailAddr(userEntity);
        }catch (Exception e){
            log.error("选择性更新用户表的过程出现异常:{}",e.toString());
            return false;
        }
    }

    public boolean userDeregist(String emailAddr){
        try{
            log.info("开始用户删除操作");
            return userMapper.userDeregist(emailAddr);
        }catch (Exception e){
            log.error("用户销户过程出现异常:{}",e.toString());
        }
        return false;
    }
}
